package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.Doctor;
import com.dgut.doctorservice.entity.vo.DoctorQuery;
import com.dgut.doctorservice.service.DoctorService;
import com.dgut.servicebase.exceptionhandler.DoctorException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.print.Doc;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-01
 */
@RestController
@RequestMapping("/doctorservice/doctor")
@Api("医生")
public class DoctorController {

    @Autowired
    public DoctorService doctorService;

    //查询医生表所有数据
    @ApiOperation("查找所有医生")
    @GetMapping("findAll")
    public Result findAllDoctor(){
        List<Doctor> doctors = doctorService.list();
        //异常模拟演示
        //int i=10/0;

        //手动抛出才能把异常调用
//        try{
//            int i=10/0;
//        }catch (Exception e){
//            throw new DoctorException(20001,"执行了自定义异常");
//        }

        return Result.succ().data("items",doctors);
    }

    //逻辑删除医生
    @DeleteMapping("{id}")
    public Result removeDoctor(@PathVariable String id){

        boolean flag = doctorService.removeById(id);
        if (flag){
            return Result.succ();
        }else {
            return Result.fail();
        }

    }

    @GetMapping("pageDoctors/{current}/{limit}")
    public Result pageListDoctors(@PathVariable long current,@PathVariable long limit){

        Page<Doctor> page=new Page<>(current,limit);
        doctorService.page(page,null);
        long total=page.getTotal();
        List<Doctor> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return Result.succ().data(map);

    }

    //条件查询带分页
    @PostMapping ("pageDoctorsCondition/{current}/{limit}")
    public Result pageDoctorsCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) DoctorQuery doctorQuery){
        Page<Doctor> page=new Page<>(current,limit);
        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();

        //多条件组合查询
        String name = doctorQuery.getName();
        Integer level = doctorQuery.getLevel();
        String begin = doctorQuery.getBegin();
        String end = doctorQuery.getEnd();
        if (StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (StringUtils.isEmpty(begin)){
            wrapper.eq("gmt_create",begin);
        }
        if (StringUtils.isEmpty(end)){
            wrapper.like("gmt_update",end);
        }

        doctorService.page(page,wrapper);
        long total=page.getTotal();
        List<Doctor> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return Result.succ().data(map);
    }


    //添加医生的方法
    @PostMapping("addDoctor")
    public Result addDoctor(@RequestBody Doctor doctor){
        boolean save = doctorService.save(doctor);
        if (save){
            return Result.succ();
        }else {
            return Result.fail();
        }
    }

    //根据id进行查询
    @GetMapping("GetDoctorById/{id}")
    public Result getDoctorById(@PathVariable String id){
        Doctor doctor = doctorService.getById(id);
        return Result.succ().data("doctor",doctor);
    }

    @PostMapping("updateDoctor")
    public Result updateDoctor(@RequestBody Doctor doctor){
        boolean flag = doctorService.updateById(doctor);
        if (flag){
            return Result.succ();
        }else {
            return Result.fail();
        }

    }




}

